<template>
  <el-menu
    style="width: 200px"
    active-text-color="#ffd04b"
    background-color="#545c64"
    class="el-menu-vertical-demo"
    default-active="2"
    text-color="#fff"
  >
    <el-sub-menu index="1">
      <template #title>
        <span>Navigator One</span>
      </template>

      <!-- 将循环放到 template 身上 -->
      <template v-for="item in list" :key="item.name">
        <!-- 
          判断当前 item 有没有 children 
            有的话，  渲染 el-sub-menu
            没有的话，渲染 el-menu-item
            
        -->
        <el-sub-menu v-if="item.children?.length" :index="item.name">
          <template #title>
            <span>{{ item.name }}</span>
          </template>

          <el-menu-item v-for="xxx in item.children" :key="xxx.name">
            {{ xxx.name }}
          </el-menu-item>
        </el-sub-menu>
        <el-menu-item v-else>
          {{ item.name }}
        </el-menu-item>
      </template>

      <!-- 
      <el-menu-item index="1-2">item two</el-menu-item>  
      <el-menu-item index="1-2">item two</el-menu-item>
      <el-menu-item index="1-3">item three</el-menu-item>
      <el-sub-menu index="1-4">
        <template #title>item four</template>
        <el-menu-item index="1-4-1">item one</el-menu-item>
      </el-sub-menu> 
    -->
    </el-sub-menu>
  </el-menu>
</template>

<script lang="ts" setup>
const list = [
  {
    name: 'item-one'
  },
  {
    name: 'item-two'
  },
  {
    name: 'item-three'
  },
  {
    name: 'item-four',
    children: [
      {
        name: 'item-four-one'
      }
    ]
  }
]
</script>

<style lang="scss">
@import './global.scss';
</style>
